package com.zjf.addresschk.service;

import com.alibaba.fastjson.JSONObject;
import com.zjf.addresschk.entity.AddressEntity;
import com.zjf.addresschk.entity.InfAbstract;
import com.zjf.addresschk.entity.InfBaidu;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by windows on 2015/5/20.
 */
public class AddressChkServiceBaidu extends  AddressChkServiceBase{
    private InfBaidu inf;

    public AddressChkServiceBaidu(InfBaidu inf) {
        this.inf = inf;
    }

    @Override
    public void checkAddress(List<AddressEntity> addressList){
        int i = 1;
        for(AddressEntity a:addressList){
           try {
               String address = a.getAddress();
               inf.setAddress(address);
               String result = sendToHttp(getUrl(inf.getInterfaceUrl(),address, inf.getAk(),inf.getSn()));
               a.setChkResult(analysis(result));
               System.out.println("第"+i+++"个检测完成");
           }catch (Exception e){
               e.printStackTrace();
           }
        }
    }
    @Override
    public void catchPostcode(List<AddressEntity> addressList) {
        Document doc;
        int i = 1;
        for(AddressEntity a:addressList) {
            try {
                doc = Jsoup.connect("http://opendata.baidu.com/post/s?wd=" + URLEncoder.encode(a.getAddress(), "gbk") + "&p=mini&rn=20").get();
                Elements ListDiv = doc.getElementsByAttributeValue("class", "table-list");
                List<String> postcodes = new ArrayList<String>(0);
                for (Element element : ListDiv) {
                    Elements trs = element.getElementsByTag("tr");
                    for (Element tr : trs) {
                        try {
                            postcodes.add(tr.getElementsByTag("td").get(1).html().replace("<em>", "").replace("</em>", "") + "：" + tr.getElementsByTag("td").get(0).html());
                        } catch (Exception e1) {
                            // e1.printStackTrace();
                        }
                    }
                }
                a.setPostcode(postcodes.toString());
                System.out.println("第"+i+++"个抓取邮编完成");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    private String analysis(String result) {
        JSONObject o = JSONObject.parseObject(result);
        String status =o.getString("status");
        if("0".equals(status)){
            //0表示成功
            return "有效,百度坐标:"+((JSONObject)o.get("result")).get("location").toString();
        }else{
            //表示无效
            return "无效";
        }
    }

    private String getUrl(String interfaceUrl, String address, String ak, String sn) {
        interfaceUrl = interfaceUrl.replace("{address}",URLEncoder.encode(address));
        interfaceUrl += "&sn="+sn;
        return interfaceUrl;
    }



}
